//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.wltea.analyzer.dic;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;

public class Monitor implements Runnable {

    private static CloseableHttpClient httpclient = HttpClients.createDefault();
    private String last_modified;
    private String eTags;
    private String location;
    private DictType dictType;

    public Monitor(DictType dictType, String location) {
        this.location = location;
        this.last_modified = null;
        this.eTags = null;
        this.dictType = dictType;
    }

    /**
     * 监控流程：
     * ①向词库服务器发送Head请求
     * ②从响应中获取Last-Modify、ETags字段值，判断是否变化
     * ③如果未变化，休眠1min，返回第①步
     * ④如果有变化，重新加载词典
     * ⑤休眠1min，返回第①步
     */
    public void run() {
        RequestConfig rc = RequestConfig.custom().setConnectionRequestTimeout(10000).setConnectTimeout(10000).setSocketTimeout(15000).build();
        HttpHead head = new HttpHead(this.location);
        head.setConfig(rc);
        if (this.last_modified != null) {
            head.setHeader("If-Modified-Since", this.last_modified);
        }

        if (this.eTags != null) {
            head.setHeader("If-None-Match", this.eTags);
        }

        CloseableHttpResponse response = null;

        try {
            response = httpclient.execute(head);
            System.out.println("============" + location + "=======================");
            System.out.println(response.toString());
            System.out.println("============" + location + "=======================");
            if (response.getStatusLine().getStatusCode() == 200) {
                if (!response.getLastHeader("Last-Modified").getValue().equalsIgnoreCase(this.last_modified)
                        || !response.getLastHeader("ETag").getValue().equalsIgnoreCase(this.eTags)) {
                    Dictionary.getSingleton().reLoadRemoteDict(dictType, location);
                    this.last_modified = response.getLastHeader("Last-Modified") == null ? null : response.getLastHeader("Last-Modified").getValue();
                    this.eTags = response.getLastHeader("ETag") == null ? null : response.getLastHeader("ETag").getValue();
                }
            } else if (response.getStatusLine().getStatusCode() != 304) {
                System.err.println("remote_ext_dict " + this.location + " return bad code " + response.getStatusLine().getStatusCode() + "");
            }
        } catch (Exception var13) {
            System.err.println("remote_ext_dict  error!" + var13.getMessage());
            var13.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException var12) {
                var12.printStackTrace();
            }

        }

    }
}
